package com.example.userpointmgr.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class WeChatServiceImpl {
    private static final String APP_ID="wx170dd8fb51d36f15";
    private static final String APP_SECRET="c7a50462df459fed33c67a9177b6a3f1";

    public static String getSessionKey(String code) {
        String urlString=String.format("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code",APP_ID,APP_SECRET,code);
        System.out.println("请求地址对不对："+urlString);
        try{
            URL url=new URL(urlString);
            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String inputLIne;
            StringBuilder content=new StringBuilder();
            while((inputLIne=reader.readLine())!=null){
                content.append(inputLIne);
            }
            reader.close();
            connection.disconnect();
            String response=content.toString();
            return response;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
